IF OBJECT_ID('ssf.job_run_datetime') IS NOT NULL
	DROP FUNCTION ssf.job_run_datetime
GO

CREATE FUNCTION ssf.job_run_datetime (
	@run_date INT
	, @run_time INT
)
RETURNS DATETIME
AS
BEGIN
/*
<object_header>

<object_summary>
	<purpose><![CDATA[
SQL Agent records job and step start date and time in sysjobhistory as integers.
Given integer values representing a date and time, return the converted datetime.
	]]></purpose>
</object_summary>

</object_header>
*/

	IF @run_date IS NULL
		RETURN NULL
	
	RETURN (
		STUFF(STUFF(CAST(@run_date AS CHAR(8)), 5, 0, '-'), 8, 0, '-')
		+ SPACE(1)
		+ STUFF(STUFF(CAST(RIGHT(REPLICATE('0', 6) + ISNULL(CAST(@run_time AS VARCHAR(6)), ''), 6) AS CHAR(6)), 3, 0, ':'), 6, 0, ':')
	)
END
GO
